package com.prs.crm.service.oa.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.AbstractDataset;
import org.jfree.data.general.DefaultPieDataset;

import com.prs.crm.context.Attach;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.context.ToolKits;
import com.prs.crm.dao.oa.ApplyDao;
import com.prs.crm.domain.flow.WorkFlowProcessNode;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.Apply;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.impl.FlowServiceImpl;
import com.prs.crm.service.oa.ApplyService;

public class ApplyServiceImpl extends FlowServiceImpl<Apply,ApplyDao> implements ApplyService{
	private PersonService personService;
	
	@Override
	public void saveApply(Apply apply, File file, String fileName,String copyToPersons) {
		// TODO Auto-generated method stub
		Attach attach = FileUtil.getInstance().saveFile(file, fileName, "", Constrains.UPLOAD_DIC_APPLY);
		if(attach.getSrc() != null) {
			apply.setAttach(attach.getSrc());
		}
		
		Collection<Person> copyPersons = this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(copyToPersons.split(",")));
		apply.addCopyToPersons(copyPersons);

		this.getDao().save(apply);
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public PersonService getPersonService() {
		return personService;
	}

	@Override
	public boolean valideSubmit(Apply entity)
	{
		return true;
	}
	
	@Override
	public Collection<Apply> getAllApply() {
		// TODO Auto-generated method stub
		return this.getDao().getAllApply();
	}

	@Override
	public Collection<Apply> getApplyByIds(Integer[] ids) {
		// TODO Auto-generated method stub
		return this.getDao().getApplyByIds(ToolKits.convertToIntAry(ids));
	}

	@Override
	public Collection<?> getReportApplySource(Integer state, String level,
			Date datestart, Date dateend, Collection<Person> persons) {
		// TODO Auto-generated method stub
		return this.getDao().getReportApplySource(state, level, datestart, dateend, persons);
	}
	@SuppressWarnings("unchecked")
	@Override
	public AbstractDataset fillDataset(Collection<?> dataList, int GraphicsType) {
		// TODO Auto-generated method stub
		AbstractDataset Dataset = null;
		if(GraphicsType == Constrains.REPORT_PIECHART)
			Dataset = new DefaultPieDataset();
		else if(GraphicsType == Constrains.REPORT_BARCHART)
			Dataset = new DefaultCategoryDataset();
		
		for(Object[] o : (Collection<Object[]>)dataList){
			String name = "";
			Number count = 0;
			for(Object value : o){
				if(value != null){
					if(value instanceof Person){
						name = ((Person) value).getResume().getName();
					}else if(value instanceof Number) {
						count = (Number)value;
					}
				}
			}
			if(!name.equals("")){
				if(GraphicsType == Constrains.REPORT_PIECHART)
					((DefaultPieDataset)Dataset).setValue(name, count);
				else if(GraphicsType == Constrains.REPORT_BARCHART)
					((DefaultCategoryDataset)Dataset).addValue(count, name, name);
			}
		}
		return Dataset;
	}

	@Override
	public Collection<Apply> getValidCritiqueApply(Person person) {
		// TODO Auto-generated method stub
		return this.getDao().getValidCritiqueApply(person);
	}

	@Override
	public Collection<WorkFlowProcessNode> getHistoryFlowNode(Apply apply) {
		// TODO Auto-generated method stub		
		return this.getWorkflowProcessService().pageHistoryFlowNode(apply);
	}

	@Override
	public Collection<Apply> getExistAuditInfoByPerson(Person person) {
		// TODO Auto-generated method stub
		return this.getDao().getExistAuditInfoByPerson(person);
	}
}
